import { Component,OnInit,NgModule } from '@angular/core';
import {SelectService} from './select.service';
import {Data} from './items';
@Component({
  selector: 'app-tip3',
  templateUrl: './app.component.html',
  styleUrls:[`./tip3.component.css`]
})

export class Tip3Component  implements OnInit{

      loading: boolean;
      //所有的数据
      items: Data[] = [];
      //selectedItem存放被选中的类目，是data类型
      selectedItem: Data = {};
      //selectedsubitems存放被选中的子类   Set是数据结构，有add和delette方法
      selectedSubItems: Set<Data> = new Set();
      //显示状态
      active:Data[];
      constructor(private selectService: SelectService) { }

      ngOnInit() {
        this.loading = true;
        this.selectService.getSelectService().subscribe(data => {
            //console.log(data);
            this.items = <Data[]>data;
            this.loading = false;
            this.selectedItem=this.items[0];
        });
      }
// 如果他们是一样的item，就清空被选中的item，如果不一样就替换被选中的item

    toggleItem(willSelectedItem) {
      if (this.selectedItem.id === willSelectedItem.id) {
        this.selectedItem = {}
      }
      else {
        this.selectedItem = willSelectedItem;
        //默认显示第一个第一级下内容
      }
    }
    //isAddclass(willSelectedItem):Boolean{
      //return this.selectedItem.has(willSelectedItem);
  //  }

    //假设当前二级菜单数据被点击，那么就删除；没有被点击就添加上
      selectSubitem(subitem) {
        if(this.selectedSubItems.has(subitem)){
          this.selectedSubItems.delete(subitem);
        //  console.log(subitem);
        }
        else {
          this.selectedSubItems.add(subitem);
          this.selectedSubItems.delete(this.active);
        //  console.log(subitem);
        }
      }
      //判断subItem是否被选择  如果被选中为真 添加subitme类的样式
      isIncludes(subItem):Boolean{
        return this.selectedSubItems.has(subItem);
      }
}
